main
1import { getCollection } from "astro:content";
2import type { APIRoute, GetStaticPaths } from "astro";
3import type { Locale } from "intlayer";
4
5import { SITE_CONFIG } from "@/config.ts";
6import { createFeed, localePostUrl } from "@/utils/feed.ts";
7import { getLocaleFeedUrl, getRelativeLocaleUrl, url } from "@/utils/url.ts";
8
9export const getStaticPaths = (() => {
10 return SITE_CONFIG.locales.map((locale) => ({ params: { locale } }));
11}) satisfies GetStaticPaths;
12
13export const GET: APIRoute = async ({ site, params }) => {
14 if (!site) {
15 throw new Error("`site` must be configured in astro.config to build feeds");
16 }
17 const origin = site.origin;
18 const locale = params.locale as Locale;
19 const posts = (await getCollection("posts")).filter((post) =>
20 post.id.startsWith(`${locale}/`),
21 );
22 const feed = await createFeed({
23 id: new URL(getRelativeLocaleUrl("/", locale), origin).toString(),
24 feedUrl: new URL(getLocaleFeedUrl(locale), origin).toString(),
25 favicon: new URL(url("/favicon.ico"), origin).toString(),
26 title: SITE_CONFIG.title,
27 description: SITE_CONFIG.description[locale],
28 language: locale,
29 site: origin,
30 posts,
31 postUrl: (post) => localePostUrl(post, origin),
32 });
33
34 return new Response(feed.atom1(), {
35 headers: { "Content-Type": "application/atom+xml" },
36 });
37};